###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# Libraries/Mail.pm.pl -> Mail library                                        #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Mail;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY {}

###############################################################################
# Send subroutine
sub Send {
  my $self = shift;
  my %in = ("TO" => "", @_);

  $self->{'ERROR'} = "No recipient" and return if (!$in{'TO'});

  if ($SYSTEM->{'MAIL_TYPE'} eq "SENDMAIL") {
    $self->Sendmail(@_) and return 1;
  } else {
    $self->SMTP(@_) and return 1;
  }
}

###############################################################################
# Sendmail subroutine (internal)
sub Sendmail {
  my $self = shift;
  my %in = (
    "TO"      => "",
    "FROM"    => "\"$GENERAL->{'LINKS_TITLE'}\" <$GENERAL->{'CONTACT_EMAIL'}>",
    "CC"      => "",
    "BCC"     => "",
    "SUBJECT" => "",
    "HEADERS" => {},
    "HTML"    => 0 ,
    "MESSAGE" => "",
    @_
  );

  if ($in{'HTML'}) {
    $in{'HEADERS'}->{'Content-Type'} ||= "text/html; charset=\"$SYSTEM->{'CHARSET'}\"";
  } else {
    $in{'HEADERS'}->{'Content-Type'} ||= "text/plain; charset=\"$SYSTEM->{'CHARSET'}\"";
  }

  # Filter inputs
  $in{'TO'}  =~ s/[ \t]+/ /g;
  $in{'CC'}  =~ s/[ \t]+/ /g;
  $in{'BCC'} =~ s/[ \t]+/ /g;

  $in{'TO'}  =~ s/,,/,/g;
  $in{'CC'}  =~ s/,,/,/g;
  $in{'BCC'} =~ s/,,/,/g;
  
  $in{'SUBJECT'} = &Standard::Textize($in{'SUBJECT'});
  $in{'MESSAGE'} = &Standard::Textize($in{'MESSAGE'}) unless ($in{'HEADERS'}{'Content-Type'} =~ /^text\/html/i);

  # Send email
  open(MAIL, "|$SYSTEM->{'SENDMAIL'} -t") || ($self->{'ERROR'} = $! and return);
  print MAIL "To: $in{'TO'}\n";
  print MAIL "From: $in{'FROM'}\n";
  print MAIL "Bcc: $in{'BCC'}\n" if $in{'BCC'};
  print MAIL "Cc: $in{'CC'}\n" if $in{'CC'};

  foreach $Key (keys %{ $in{'HEADERS'} }) {
    print MAIL $Key . ": " . $in{'HEADERS'}{$Key} . "\n" if $in{'HEADERS'}{$Key};
  }

  print MAIL "X-Mailer: SuperLinks\n";
  print MAIL "X-Mailer-Info: http://www.plasmapulse.com\n";
  print MAIL "Subject: $in{'SUBJECT'}\n" if $in{'SUBJECT'};
  print MAIL "\n";
  print MAIL "$in{'MESSAGE'}\n\n" if $in{'MESSAGE'};
  close(MAIL);

  return 1;
}

###############################################################################
# SMTP subroutine (internal)
sub SMTP {
  my $self = shift;
  my %in = (
    "TO"      => "",
    "FROM"    => "$GENERAL->{'LINKS_TITLE'} <$GENERAL->{'CONTACT_EMAIL'}>",
    "CC"      => "",
    "BCC"     => "",
    "SUBJECT" => "",
    "HEADERS" => {},
    "HTML"    => 0 ,
    "MESSAGE" => "",
    @_
  );

  if ($in{'HTML'}) {
    $in{'HEADERS'}->{'Content-Type'} ||= "text/html; charset=\"$SYSTEM->{'CHARSET'}\"";
  } else {
    $in{'HEADERS'}->{'Content-Type'} ||= "text/plain; charset=\"$SYSTEM->{'CHARSET'}\"";
  }

  # Filter inputs
  $in{'SUBJECT'} = &Standard::Textize($in{'SUBJECT'});
  $in{'MESSAGE'} = &Standard::Textize($in{'MESSAGE'}) unless ($in{'HEADERS'}{'Content-Type'} =~ /^text\/html/i);

  use Mail::Sendmail;

  unshift(@{ $Mail::Sendmail::mailcfg{'smtp'} }, $SYSTEM->{'SMTP_SERVER'});

  &sendmail(
    To              => $in{'TO'},
    From            => $in{'FROM'},
    Cc              => $in{'CC'},
    Bcc             => $in{'BCC'},
    Subject         => $in{'SUBJECT'},
    %{ $in{'HEADERS'} },

    "X-Mailer"      => "SuperLinks",
    "X-Mailer-Info" => "http://www.plasmapulse.com",

    Message         => $in{'MESSAGE'}
  ) || ($self->{'ERROR'} = $Mail::Sendmail::error and return);

  return 1;
}

1;